package de.tabuma.formcreator;

import java.util.*;
import java.util.logging.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import de.tabuma.formcreator.entity.*;
import de.tabuma.formcreator.logik.*;
import de.tabuma.formcreator.xml.logik.*;
import de.tabuma.soap.entity.wsdl.*;

/**
  * @author $Author: alefen $
  * @version $Revision: 1.1.1.1.6.1 $
  * @since $Date: 2010/01/13 10:12:51 $
 */
public class dataservlet extends HttpServlet {

    private static final Logger log = Logger.getLogger(dataservlet.class.getName());

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        log.finest("dataservlet startet");
        Enumeration enu = request.getParameterNames();
        while(enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            String value = request.getParameter(key);
            log.finest(key+":"+value);

        }

        try {
            String xmlStr = request.getParameter(constants.xmlParameter);
            log.finest(constants.xmlParameter+":"+xmlStr);
            int projectID = Integer.valueOf(request.getParameter(de.tabuma.formcreator.constants.projectParameter)).intValue();

            String method = request.getParameter(de.tabuma.formcreator.constants.methodParameter);
            
            log.fine("loading wsdl file from project");
            //1. Schritt laden der Url
            TDefinitions wsdl = de.tabuma.soap.logik.PMF.get(projectID, new TDefinitions());

            log.fine("loading formular manager ("+projectID+","+method+")");
            //2. Schritt Initialisierung des FormularManagers
            FormManager formularManager = FormManagerFactory.createFormularManager(wsdl, method);
            
            log.fine("sending data");
            out.println(XmlFacade.doRequest(wsdl, method, xmlStr));

        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}